package com.utils.fileLoader
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	import com.utils.movieClipToBitmap.MovieClipToBitmapData;
	
	public class WindowSelectLoadFile extends Sprite
	{
		public static const instance:WindowSelectLoadFile = new WindowSelectLoadFile();
		private var _fileReference:FileReference;
		
		private var _loader:Loader;
		private var _loaderContent:MovieClip;//mc容器
		private var _curMovieClip:MovieClip;//当前的mc
		private var _curFileName:String;
		public function WindowSelectLoadFile()
		{
			if(instance) throw Error('不能重复实例化!');
		}
		
		public function openWindow():void
		{
			if(!_fileReference) _fileReference = new FileReference();
			_fileReference.browse([new FileFilter('swf文件','*.swf')]);
			_fileReference.addEventListener(Event.COMPLETE,onFileReferenceCompleteHandler);
			_fileReference.addEventListener(IOErrorEvent.IO_ERROR,onIoErrorHandler);
			_fileReference.addEventListener(Event.SELECT,onStartLoadHandler);
		}
		
		private function onIoErrorHandler(e:IOErrorEvent):void
		{
			trace('IOErrorEvent:' + e.text);
		}
		
		/**FileReference加载完毕执行*/		
		private function onFileReferenceCompleteHandler(e:Event):void
		{
			//trace('文件类型:' + _fileReference.type,'文件名:' + _fileReference.name);
			_curFileName = _fileReference.name.split('.')[0];
			if(_fileReference.data is ByteArray){
				//图片或swf等二进制数据 需要借助Loader转成视图
				_loader = new Loader(); 
				_loader.loadBytes(_fileReference.data);//加载二进制数据，转化为实体
				if(!_loader.contentLoaderInfo.hasEventListener(Event.COMPLETE)){
					_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadCompleteHandler);
				}
				if(!_loader.contentLoaderInfo.hasEventListener(IOErrorEvent.IO_ERROR)){
					_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIoErrorHandler);
				}
			}else{
				trace(_fileReference.data);
			}
		}		
		
		/**load加载完毕执行*/		
		private function onLoadCompleteHandler(e:Event):void
		{
			_loaderContent = _loader.content as MovieClip;
			MovieClipToBitmapData.instance.dataSwitch(_loaderContent,_curFileName,true);
		}
		
		/**点击选择按钮后开始加载文件*/		
		private function onStartLoadHandler(e:Event):void
		{
			_fileReference.load();
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}